package com.itheima.tanhua.controller;

import com.itheima.tanhua.service.MessagesService;
import com.itheima.tanhua.vo.PageResult;
import com.itheima.tanhua.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author Lff
 * @date 2022/3/31
 * 即时消息处理
 */
@RestController
@RequestMapping("/messages")
public class MessagesController {

    @Autowired
    private MessagesService msgService;

    /**
     * 获取用户详细信息
     */
    @GetMapping("/userinfo")
    public ResponseEntity getUserInfoByHX(@RequestParam(value = "huanxinId", required = true)
                                                  String hxUser) {
        //查询并返回
        UserInfoVo userInfoVo = msgService.findUserInfoByHx(hxUser);
        return ResponseEntity.ok(userInfoVo);
    }

    /**
     * 同意好友申请
     *
     * @param map
     * @return
     */
    @PostMapping("/contacts")
    public ResponseEntity contacts(@RequestBody Map map) {
        String userIdStr = map.get("userId").toString();
        Long userId = Long.valueOf(userIdStr);
        msgService.agreeContacts(userId);
        return ResponseEntity.ok(null);
    }


    /**
     * 获取联系人列表
     *
     * @param page
     * @param pagesize
     * @param keyword
     * @return
     */
    @GetMapping("/contacts")
    public ResponseEntity getContactsList(@RequestParam(defaultValue = "1") Integer page,
                                          @RequestParam(defaultValue = "10") Integer pagesize,
                                          @RequestParam(required = false) String keyword) {
        PageResult pr = msgService.findContactsList(page, pagesize, keyword);
        return ResponseEntity.ok(pr);
    }

    /**
     * 查看公告信息
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/announcements")
    public ResponseEntity announcements(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = msgService.getAnnouncements(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 查看喜欢列表
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/loves")
    public ResponseEntity loves(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = msgService.getLoves(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 查看点赞列表
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/likes")
    public ResponseEntity likes(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = msgService.getLoves(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 查看评论列表
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/comments")
    public ResponseEntity comments(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = msgService.getLoves(page, pagesize);
        return ResponseEntity.ok(pr);
    }
}
